package com.ruoyi.customer.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.customer.domain.TotalComster;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author suntf
 * @since 2023-08-24
 */
public interface TotalComsterMapper extends BaseMapper<TotalComster> {

    @Update("<script> " +
            " update total_comster set customer_flag = 1 where id = #{id} and del_flag = 0 and  <![CDATA[  customer_flag <> '1' ]]> " +
            "</script>")
    Integer updateCustomer(@Param("id") Long id);

    /**
     * 一键下发更新 customer_flag=1  下发到公司池
     * del_flag = 0  存在总库存在但是已经删除的数据
     * @param tel
     * @return
     */
    @Update("<script> " +
            " update total_comster set customer_flag = '1', del_flag = '0' where mobile = #{tel}" +
            "</script>")
    Integer updateIssueCustomer(@Param("tel") String tel);


    @Update("update ${tableName} a left join total_comster b on  a.tel = b.mobile set a.bz = #{remark}, b.remark  = #{remark}, del_flag = 0 where  a.id = ${id}")
    void updateTotalRemark(@Param("id") Long id, @Param("remark") String remark, @Param("tableName") String tableName);


    @Update("update ${tableName} a left join total_comster b on  a.tel = b.mobile set a.name = #{customerName}, b.name  = #{customerName}, del_flag = 0 where  a.id = ${id}")
    void updateTotalCustomerName(@Param("id") Long id, @Param("customerName") String customerName, @Param("tableName") String tableName);

    @Update("update ${tableName} a left join total_comster b on  a.tel = b.mobile set a.level = #{customerLevel}, b.level  = #{customerLevel}, del_flag = 0 where  a.id = ${id}")
    void updateTotalCustomerLevel(@Param("id") Long id, @Param("customerLevel") String customerLevel, @Param("tableName") String tableName);

    @Update("update ${tableName} a left join total_comster b on  a.tel = b.mobile set b.customer_status  = #{customerStatus}, del_flag = 0 where  a.id = ${id}")
    void updateTotalCustomerStatus(@Param("id") Long id, @Param("customerStatus") String customerStatus, @Param("tableName") String tableName);


    @Update("update ${tableName} a, t_customer  b set b.bz = #{remark} where a.tel = b.tel and a.id = ${id}")
    Integer updateCustomerRemark(@Param("id") Long id, @Param("remark") String remark, @Param("tableName") String tableName);

    @Update("update ${tableName} a, t_customer  b set b.name = #{customerName} where a.tel = b.tel and a.id = ${id}")
    Integer updateCustomerName(@Param("id") Long id, @Param("customerName") String customerName, @Param("tableName") String tableName);

    @Update("update ${tableName} a, t_customer  b set b.level = #{customerLevel} where a.tel = b.tel and a.id = ${id}")
    Integer updateCustomerLevel(@Param("id") Long id, @Param("customerLevel") String customerLevel, @Param("tableName") String tableName);

    @Update("update ${tableName} a, t_customer  b set b.customer_status = #{customerStatus} where a.tel = b.tel and a.id = ${id}")
    Integer updateCustomerStatusNew(@Param("id") Long id, @Param("customerStatus") String customerStatus, @Param("tableName") String tableName);

    @Update("update t_customer b left join total_comster a on a.customer_flag != 2 and a.id = b.total_customer_id \n" +
            " SET a.customer_flag = 2, a.return_msg = #{msg},\n" +
            " b.deleted = 1, b.update_time = now(), b.update_by = #{updateBy}, a.remark = b.bz \n" +
            "WHERE b.id = #{id} AND b.deleted = 0 and b.is_reviewe = -1")
    Integer updateCustomerStatus(@Param("id") Long id, @Param("msg") String msg, @Param("updateBy") String updateBy);


    @Update("update total_comster set remark = concat(#{remark}, remark) , name = concat(#{name}, name) where mobile = #{tel}")
    Integer updateTotalRemarkName(@Param("tel") String tel, @Param("remark") String remark, @Param("name") String name);
}
